package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ClassBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClassServiceImp implements IClassService{
    @Autowired
    private IClassMapper classMapper;
    @Autowired
    private IStudentMapper studentMapper;

    @Override
    public List<ClassBean> findAll() {
        return classMapper.selectList(null);
    }

    @Override
    public boolean add(ClassBean classBean) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("c_classname",classBean.getClassName());
        List list = classMapper.selectList(qw);
        if (list.size() == 0){
            classMapper.insert(classBean);
            return true;
        }
        return false;
    }

    @Override
    public boolean del(Integer classID) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("fk_classid",classID);
        List list = studentMapper.selectList(qw);
        if (list.size() == 0) return false;
        classMapper.deleteById(classID);
        return true;
    }
}
